import Cesium from '../Ces/Cesium'

class InternalHandler {
  static Type = {
    pickEllipsoid: 0,
  }
  _scene

  _handlerMove
  _handlerDown
  _handlerUp
  _moveEvent
  _downEvent
  _upEvent
  _pickEvent
  _lastPos
  constructor(viewer) {
    this._scene = viewer.scene;
  }

  get moveEvent() {
    if (!Cesium.defined(this._moveEvent)) {
      this._moveEvent = new Cesium.Event();
      this.startMove();
    }

    return this._moveEvent;
  }

  get downEvent() {
    if (!Cesium.defined(this._downEvent)) {
      this._downEvent = new Cesium.Event();
      this.startDown();
    }

    return this._downEvent;
  }
  get upEvent() {
    if (!Cesium.defined(this._upEvent)) {
      this._upEvent = new Cesium.Event();
      this.startUp();
    }

    return this._upEvent;
  }
  get pickEvent() {
    if (!Cesium.defined(this._pickEvent)) {
      this._pickEvent = new Cesium.Event();
      this.startPick();
    }

    return this._pickEvent;
  }

  destroy() {
    this._handlerMove = this._handlerMove && this._handlerMove.destroy();
    this._handlerDown = this._handlerDown && this._handlerDown.destroy();
  }

  startMove() {
    const that = this;
    const scene = this._scene;

    // on mouse move
    this._handlerMove = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    this._handlerMove.setInputAction((movement) => {
      const cartesian = scene.camera.pickEllipsoid(
        movement.endPosition,
        scene.globe.ellipsoid,
      );

      if (Cesium.defined(cartesian)) {
        that._moveEvent.raiseEvent(
          cartesian,
          InternalHandler.Type.pickEllipsoid,
          movement.endPosition,
          '',
        );
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  startDown() {
    const that = this;
    const scene = this._scene;

    // on mouse down
    this._handlerDown = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    this._handlerDown.setInputAction((movement) => {
      if (Cesium.defined(that._downEvent)) {
        const cartesian = scene.camera.pickEllipsoid(
          movement.position,
          scene.globe.ellipsoid,
        );

        if (Cesium.defined(cartesian)) {
          that._downEvent.raiseEvent(
            cartesian,
            InternalHandler.Type.pickEllipsoid,
            movement.position,
            '',
          );
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
  }

  startUp() {
    const that = this;
    const scene = this._scene;

    // on mouse up
    this._handlerUp = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    this._handlerUp.setInputAction((movement) => {
      if (Cesium.defined(that._upEvent)) {
        const cartesian = scene.camera.pickEllipsoid(
          movement.position,
          scene.globe.ellipsoid,
        );

        if (Cesium.defined(cartesian)) {
          that._upEvent.raiseEvent(
            cartesian,
            InternalHandler.Type.pickEllipsoid,
            movement.position,
            '',
          );
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_UP);
  }

  startPick() {
    const that = this;
    const scene = this._scene;

    // on mouse up
    this._handlerUp = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    this._handlerUp.setInputAction((movement) => {
      if (Cesium.defined(that._pickEvent)) {
        const cartesian = scene.camera.pickEllipsoid(
          movement.position,
          scene.globe.ellipsoid,
        );

        if (Cesium.defined(cartesian)) {
          that._pickEvent.raiseEvent(
            cartesian,
            InternalHandler.Type.pickEllipsoid,
            movement.position,
            '',
          );
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }
}

export default InternalHandler;

